package com.servlet;

import com.dao.ItemsRepository;
import com.entity.Cart;
import com.entity.Items;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Wuxiang on 2018/4/11
 */
public class CartServlet extends HttpServlet {

    //表示购物车的动作 ,add,show,delete
    private String action;

    //商品业务逻辑类的对象
    private ItemsRepository idao = new ItemsRepository();


    /**
     * Constructor of the object.
     */
    public CartServlet() {
        System.out.println("CartServlet 构造函数");
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy();
        System.out.println("CartServlet 销毁");
    }

    /**
     * The doGet method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");

        if (request.getParameter("action") != null) {
            this.action = request.getParameter("action");

            //如果是添加商品进购物车
            if (action.equals("add")) {
                if (addToCart(request, response)) {
                    request.getRequestDispatcher("/success.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher("/failure.jsp").forward(request, response);
                }
            }

            //如果是显示购物车
            if (action.equals("show")) {
                request.getRequestDispatcher("/cart.jsp").forward(request, response);
            }

            //如果是执行删除购物车中的商品
            if (action.equals("delete")) {
                deleteFromCart(request, response);
                request.getRequestDispatcher("/cart.jsp").forward(request, response);
            }
        }

    }

    //添加商品进购物车的方法
    private boolean addToCart(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        String number = request.getParameter("num");
        Items item = idao.getItemsById(Integer.parseInt(id));

        // 是否是第一次给购物车添加商品,需要给session中创建一个新的购物车对象
        if (request.getSession().getAttribute("cart") == null) {
            Cart cart = new Cart();
            request.getSession().setAttribute("cart", cart);
        }
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        return cart.addGoodsInCart(item, Integer.parseInt(number));

    }

    //从购物车中删除商品
    private boolean deleteFromCart(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        Items item = idao.getItemsById(Integer.parseInt(id));
        return cart.removeGoodsFromCart(item);
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        System.out.println("CartServlet init初始化");
    }
}
